﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Invoicing.Model.Products;
namespace Invoicing.Command.Products
{
    public class ProductRatioCommand
    {
        public static ProductRatioModel GetModel(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.ProductRatios.FirstOrDefault(p => p.ID == id);
                    if (d != null)
                    {
                        var ret = Convertor.ConvertTo<ProductRatioModel, ProductRatio>(d);
                        ret.Module = d.Product.Module;
                        return ret;
                    }
                }
            }
            return null;
        }

        public static void SaveCheck(ProductRatioModel model, DBCentre db)
        {
            if (model.ProductID==0)
                throw new Utils.InvoicingException("请选择一个产品。");
            if (db != null)
            {
                if (db.DataContext.ProductRatios.Any(p => p.ProductID == model.ProductID && p.MaterialID == model.MaterialID && p.Size == model.Size && p.ID != model.ID))
                    throw new Utils.InvoicingException("记录重复，请检查。");
            }
        }

        public static ProductRatioModel CreateNewRatio(ProductRatioModel model)
        {
            using (DBCentre db = new DBCentre())
            {
                SaveCheck(model, db);
                var d = Convertor.ConvertFrom<ProductRatio, ProductRatioModel>(model);
                db.DataContext.ProductRatios.Add(d);
                db.DataContext.SaveChanges();
                model.ID = d.ID;
                return model;
            }
        }

        public static void UpdateRatio(ProductRatioModel model)
        {
            using (DBCentre db = new DBCentre())
            {
                SaveCheck(model, db);
                var d = db.DataContext.ProductRatios.FirstOrDefault(p => p.ID == model.ID);
                if (d != null)
                {
                    Convertor.UpdateValue(model, d);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static void DeleteRatio(int id)
        {
            if (id > 0)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<ProductRatio>(p => p.ID == id);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static ProductRatioCollection Gets(ProductRatioQuery sea)
        {
            var ret = new ProductRatioCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<ProductRatio> q = db.DataContext.ProductRatios;
                if (sea != null)
                {
                    if (sea.ProductID > 0)
                        q = q.Where(p => p.ProductID == sea.ProductID);
                    if (sea.MaterialID > 0)
                        q = q.Where(p => p.MaterialID == sea.MaterialID);
                    if (!string.IsNullOrEmpty(sea.Size))
                        q = q.Where(p => p.Size == sea.Size);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<ProductRatioModel, ProductRatio, ProductRatioCollection>(lst);
                    ret.TotalRecordsCount = n;
                    ret.ForEach(p => {
                        var d = lst.Find(o => o.ID == p.ID);
                        p.Module = d.Product.Module;
                        p.MaterialName = d.ProductMaterial.MaterialName;
                    });
                }
            }
            return ret;
        }
    }
}
